﻿#region << 文 件 说 明 >>

/*----------------------------------------------------------------
// 文件名称：VampirewalLog
// 创 建 者：杨程
// 创建时间：2021/9/24 10:21:40
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//
//
//----------------------------------------------------------------*/

#endregion

namespace Vampirewal.Core.Components;

/// <summary>
/// VampirewalLog日志
/// </summary>
public sealed class VampirewalCoreLogService : IVampirewalCoreLogService
{
    private int LogLevel { get; set; }

    private SqlSugarRepository<Logger> Rep { get; set; }

    /// <summary>
    /// 构造函数
    /// </summary>
    public VampirewalCoreLogService(SqlSugarRepository<Logger> _rep ,IConfigContainer<AppBaseOptions> appOptions)
    {
        Rep = _rep;
        LogLevel = (int)appOptions.Value.LogLevel;
    }

    /// <summary>
    /// 写Debug日志
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="SystemModuleName"></param>
    public void DebugLog(string Msg, string SystemModuleName)
    {
        if (LogLevel >= 0)
        {
            Logger logger = new Logger()
            {
                Content = Msg,
                Type = LoggerType.Debug,
                SystemMoudleName = SystemModuleName,
                CreateTime = DateTime.Now,
                CreateBy = VampirewalCoreContext.GetInstance().GetContext("UserName", out object UserName) ? UserName.ToString() : "",
                CreateUserId = VampirewalCoreContext.GetInstance().GetContext("UserId", out object UserId) ? UserId.ToString() : "",
            };

            Rep.Insert(logger);
        }
    }

    /// <summary>
    /// 写错误日志
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="SystemModuleName"></param>
    public void ErrorLog(string Msg, string SystemModuleName)
    {
        if (LogLevel >= 2)
        {
            Logger logger = new Logger()
            {
                Content = Msg,
                Type = LoggerType.Error,
                SystemMoudleName = SystemModuleName,
                CreateTime = DateTime.Now,
                CreateBy = VampirewalCoreContext.GetInstance().GetContext("UserName", out object UserName) ? UserName.ToString() : "",
                CreateUserId = VampirewalCoreContext.GetInstance().GetContext("UserId", out object UserId) ? UserId.ToString() : "",
            };
            Rep.Insert(logger);
        }
    }

    /// <summary>
    /// SQL出现问题
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="SystemModuleName"></param>
    public void SQLLog(string Msg, string SystemModuleName)
    {
        if (LogLevel >= 3)
        {
            Logger logger = new Logger()
            {
                Content = Msg,
                Type = LoggerType.SQL,
                SystemMoudleName = SystemModuleName,
                CreateTime = DateTime.Now,
                CreateBy = VampirewalCoreContext.GetInstance().GetContext("UserName", out object UserName) ? UserName.ToString() : "",
                CreateUserId = VampirewalCoreContext.GetInstance().GetContext("UserId", out object UserId) ? UserId.ToString() : "",
            };
            Rep.Insert(logger);
        }
    }

    /// <summary>
    /// 写警告日志
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="SystemModuleName"></param>
    public void WarningLog(string Msg, string SystemModuleName)
    {
        if (LogLevel >= 1)
        {
            Logger logger = new Logger()
            {
                Content = Msg,
                Type = LoggerType.Warning,
                SystemMoudleName = SystemModuleName,
                CreateTime = DateTime.Now,
                CreateBy = VampirewalCoreContext.GetInstance().GetContext("UserName", out object UserName) ? UserName.ToString() : "",
                CreateUserId = VampirewalCoreContext.GetInstance().GetContext("UserId", out object UserId) ? UserId.ToString() : "",
            };

            Rep.Insert(logger);
        }
    }

    /// <summary>
    /// 写自定义日志
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="loggerType"></param>
    /// <param name="SystemModuleName"></param>
    public void WriteLog(string Msg, LoggerType loggerType, string SystemModuleName)
    {
        Logger logger = new Logger()
        {
            Content = Msg,
            Type = loggerType,
            SystemMoudleName = SystemModuleName,
            CreateTime = DateTime.Now,
            CreateBy = VampirewalCoreContext.GetInstance().GetContext("UserName", out object UserName) ? UserName.ToString() : "",
            CreateUserId = VampirewalCoreContext.GetInstance().GetContext("UserId", out object UserId) ? UserId.ToString() : "",
        };

        Rep.Insert(logger);
    }
}